package com.atguigu.juc.cas;


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

//100个线程，每个线程+100次，求最后的number值
public class CASDemo {
    private int number;


    public int getNumber() {
        return number;
    }

    public synchronized void  setNumber() {
        ++number;
    }

    public static void main(String[] args) throws InterruptedException {
        CASDemo casDemo = new CASDemo();
        CountDownLatch countDownLatch = new CountDownLatch(100);
        for (int i = 1; i <= 100; i++) {
            new Thread(()->{
                for (int j = 1; j <= 100 ; j++) {
                    casDemo.setNumber();
                }
                countDownLatch.countDown();
            }
            ).start();
        }

        countDownLatch.await();
        //try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); }
        System.out.println(Thread.currentThread().getName() + "最后的结果：" + casDemo.getNumber());
    }

}
